package com.yikaiye.android.yikaiye.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.utils.CommonUtil;
import com.yikaiye.android.yikaiye.utils.ScreenUtil;

/**
 * 实名认证弹窗
 */
public abstract class VerifiedDialog {
    private Dialog dialog;

    public VerifiedDialog(Activity activity) {
        if (dialog == null) {
            getDialog(activity);
        }
    }

    private void getDialog(Activity activity) {
        View view =
                activity.getLayoutInflater().inflate(R.layout.dialog_verified, null);
        dialog = new Dialog(activity, R.style.prompt_progress_dialog_dimenabled);
        dialog.setContentView(view, new LinearLayout.LayoutParams
                (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        dialog.setCancelable(true);
        Window dialogWindow = dialog.getWindow();
        // 去除遮罩效果
//        dialogWindow.setDimAmount(0f);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.TOP);
        // lp.x = 266; // 新位置X坐标
         lp.y = ScreenUtil.dip2px(100); // 新位置Y坐标
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialogWindow.setAttributes(lp);

        ImageView iconClose = view.findViewById(R.id.icon_close);
        TextView tvStartVerify = view.findViewById(R.id.tv_start_verify);
        iconClose.setOnClickListener(v -> dismissDialog());
        tvStartVerify.setOnClickListener(v -> {
            if (CommonUtil.isCanClick()) {
                verify();
                dismissDialog();
            }
        });
    }

    public abstract void verify();

    public void showDialog() {
        dialog.show();
    }

    public void dismissDialog() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }
}
